package tests;

import java.io.FileNotFoundException;
import java.util.Date;
import java.util.List;

import gui.ConsumeXMLData;
import junit.framework.Assert;

import org.junit.Test;
import conference.*;

/**
 * 
 * @author Rob Oesch
 *
 */
public class TestUserAndXMLMethods {
	@Test
	public void testNewAuthor(){
		Author my_author = new Author();
		
		Assert.assertEquals("LastName should be blank but is not.", "", my_author.getLastName());
		Assert.assertEquals("FirstName should be blank but is not.", "", my_author.getFirstName());
		Assert.assertEquals("Email should be blank but is not.", "", my_author.getEmail());
		
		my_author.setFirstName("Rob");
		my_author.setLastName("Oesch");
		my_author.setEmail("robbyoesch@gmail.com");
		
		Assert.assertEquals("LastName should be 'Oesch' but is not.", "Oesch", my_author.getLastName());
		Assert.assertEquals("FirstName should be 'Rob' but is not.", "Rob", my_author.getFirstName());
		Assert.assertEquals("Email should be 'robbyoesch@gmail.com' but is not.", "robbyoesch@gmail.com", my_author.getEmail());
	}
	
	@Test
	public void testNewUser(){
		User my_user = new User();
		
		Assert.assertEquals("LastName should be blank but is not.", "", my_user.getLastName());
		Assert.assertEquals("FirstName should be blank but is not.", "", my_user.getFirstName());
		Assert.assertEquals("Email should be blank but is not.", "", my_user.getEmail());
		
		my_user.setFirstName("Rob");
		my_user.setLastName("Oesch");
		my_user.setEmail("robbyoesch@gmail.com");
		
		Assert.assertEquals("LastName should be 'Oesch' but is not.", "Oesch", my_user.getLastName());
		Assert.assertEquals("FirstName should be 'Rob' but is not.", "Rob", my_user.getFirstName());
		Assert.assertEquals("Email should be 'robbyoesch@gmail.com' but is not.", "robbyoesch@gmail.com", my_user.getEmail());
	}
	
	@Test
	public void testNewUserWithPredefinedValues(){
		User my_user = new User("Rob", "Oesch", "robbyoesch@gmail.com");
		
		Assert.assertEquals("LastName should be 'Oesch' but is not.", "Oesch", my_user.getLastName());
		Assert.assertEquals("FirstName should be 'Rob' but is not.", "Rob", my_user.getFirstName());
		Assert.assertEquals("Email should be 'robbyoesch@gmail.com' but is not.", "robbyoesch@gmail.com", my_user.getEmail());
	}
	
	/**
	 * Test the 'getFirstName' and 'setFirstName' methods.
	 */
	@Test
	public void testUserFirstNameMethod(){
		User my_user = 
			new User("John", 
					"Doe", 
					"nobody@nowhere.com");
		
		Assert.assertEquals(
				"FirstName should be 'John', but is not.", 
				"John", 
				my_user.getFirstName());
		
		my_user.setFirstName("Jane");
		Assert.assertEquals(
				"FirstName should be 'Jane', but is not.", 
				"Jane", 
				my_user.getFirstName());
		
		my_user = new User(
				"Tiffany", 
				"Johnson", 
				"notreal@questionmark.com");
		Assert.assertEquals("FirstName should be " + 
				"'Tiffany', but is not.", 
				"Tiffany", 
				my_user.getFirstName());
		
		my_user = new User();
		Assert.assertEquals(
				"FirstName should be blank, but is not.", 
				"", 
				my_user.getFirstName());
	}
	
	/**
	 * Test the functionality of the 'ConsumeXMLData' class.
	 * Utilize two different source files
	 * -conferences.xml
	 * -conferences_by_Dan.xml
	 */
	@Test
	public void testConsumeXMLData(){
		
		//Extract from 'conferences.xml'
		java.util.List<Conference> conferences = 
			ConsumeXMLData.extractConferences("conferences.xml", true);
		
		//Should be 2 conferences
		Assert.assertEquals(2, conferences.size());
		
		//Should be 3 manuscripts in the first conference
		Assert.assertEquals(3, 
				conferences.get(0).getAllManuscripts().length);
		
		//Should be 2 program chairs in first conference
		Assert.assertEquals(2, conferences.get(0).getProgramChairs().size());
		
		//Should be 1 program chair in second conference
		Assert.assertEquals(1, conferences.get(1).getProgramChairs().size());

		
		conferences.clear();
		
		//Extract from 'conferences_by_Dan.xml'
		conferences = 
			ConsumeXMLData.extractConferences("conferences_by_Dan.xml", true);
		
		//Should be 1 conference
		Assert.assertEquals(1, conferences.size());
		
		//Should be 3 manuscripts in the first conference
		Assert.assertEquals(20, 
				conferences.get(0).getAllManuscripts().length);
	}
	
	/**
	 * Test functionality that saves data to a new XML file
	 */
	@Test
	public void testSavingNewXMLData_OneConference(){
		ProgramChair pc = new ProgramChair("pc_FirstName", "pc_LastName", "pc_Email");
		List<ProgramChair> pcList = new java.util.ArrayList<ProgramChair>();
		pcList.add(pc);
		
		Conference conf = new Conference("Test Saving One New Conference", new Date("12/30/2011"), new Date("12/15/2011"), new Date("12/10/2011"), null, pcList);
		
		Author a = new Author("author_FirstName", "author_LastName", "author_Email");
		List<Review> reviews = new java.util.ArrayList<Review>();
		
		Review r1 = new Review("Review1", "", new Reviewer("reviewer1_LastName", "", ""));
		Review r2 = new Review("Review2", "This manuscript is reccomended", new Reviewer("reviewer2_LastName", "reviewer2_FirstName", ""));

		reviews.add(r1);
		reviews.add(r2);
		
		Manuscript m = new Manuscript(a, "Contents of Manuscript 1", "Manuscript 1 Title");
		m = new Manuscript(m, reviews);
		conf.submitManuscript(m);
		
		ConsumeXMLData.updateConference(conf, "testSavingXMLData_OneConference.xml", true);
		
		java.util.Scanner s;
		String xml = "";
		try {
			s = new java.util.Scanner(new java.io.FileReader("testSavingXMLData_OneConference.xml"));
			while(s.hasNextLine()){
				xml += s.nextLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Assert.assertEquals("<conferences date_format='MM/dd/yyyy'><conference title='Test Saving One New Conference' date='12/10/2011' manuscript_deadline='12/30/2011' review_deadline='12/15/2011'><program_chair first_name='pc_FirstName' last_name='pc_LastName' email='pc_Email'/><manuscript title='Manuscript 1 Title'><author_first_name>author_FirstName</author_first_name><author_last_name>author_LastName</author_last_name><text>Contents of Manuscript 1</text><review><reviewer_first_name>reviewer1_LastName</reviewer_first_name><reviewer_last_name></reviewer_last_name><text></text></review><review><reviewer_first_name>reviewer2_LastName</reviewer_first_name><reviewer_last_name>reviewer2_FirstName</reviewer_last_name><text>This manuscript is reccomended</text></review></manuscript></conference></conferences>", 
				xml);
	}
}
